package com.xtaller.lazy.lib.generator;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

/**   
 * @author: Taller   
 * @date:   Mar 12, 2019 2:49:10 PM   
 * @version V1.0 
 * @Description: 文件操作
 */
public class Files {
	/**
	 * 写文件
	 * @param 
	 * 		path 	文件存储路径
	 * 		sb		文件内容
	 * 		isNew	文件存在是否删除新建 true是 false否
	 * */ 
	public static void write(String path, String fileName, StringBuffer sb, Boolean isNew) {
		File file = new File(path);
		if(!file.exists()){//如果文件夹不存在
			file.mkdir();//创建文件夹
		}
		file = new File(path + "/" + fileName);
		if(isNew) {
			if(file.exists()){
	            file.delete();
	        }
			opt(file, sb);
		}else {
			if(!file.exists()){
				opt(file, sb);
	        }
		}
	}
	private static void opt(File file, StringBuffer sb) {
		FileWriter fw = null;
        BufferedWriter writer = null;
		try {
			file.createNewFile();
			fw = new FileWriter(file);
	        writer = new BufferedWriter(fw);
	        writer.write(sb.toString());

	        writer.flush();
	        writer.close();
	        fw.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
